﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using DllShepherd.SilverlightGoogleMaps.Web.Resources;

namespace DllShepherd.SilverlightGoogleMaps
{
    public class MapApplicationConfigWrapper
    {
        #region C'tor

        private readonly IDictionary<string, string> _config = new Dictionary<string, string>();

        private void InitKeys(StartupEventArgs e)
        {
            IsInitialized = false;
            foreach (string key in e.InitParams.Keys)
            {
                if (key != StringsResourceName.Name)
                    SetValue(key, HttpUtility.UrlDecode(e.InitParams[key]));
            }
            IsInitialized = true;
        }

        private MapApplicationConfigWrapper()
        {
        }

        public static MapApplicationConfigWrapper Instance
        {
            get
            {
                if (!Application.Current.Resources.Contains(MapResources.WebConfig))
                {
                    Application.Current.Resources.Add(MapResources.WebConfig, new MapApplicationConfigWrapper());
                }
                return (MapApplicationConfigWrapper)Application.Current.Resources[MapResources.WebConfig];
            }
        }

        public void Initialize(StartupEventArgs e)
        {
            InitKeys(e);
        }

        protected bool IsInitialized { get; set; }

        #endregion C'tor

        #region Config Properties

        public string BackgroundUrl
        {
            get { return GetValue("BackgroundUrl"); }
            set { SetValue("BackgroundUrl", value); }
        }

        #endregion Config Properties

        #region Private Helper Methods

        private string GetValue(string key)
        {
            if (!_config.ContainsKey(key))
                return null;
            return _config[key];
        }

        private void SetValue(string key, string value)
        {
            _config[key] = value;
        }

        #endregion Private Helper Methods
    }

    public class MapResources
    {
        public static readonly string WebConfig = "WebConfig";
    }
}
